package com.eshop.controller;

import com.eshop.model.CategoryDetail;
import com.eshop.model.Goods;
import com.eshop.model.User;
import com.eshop.service.HeaderService;
import com.eshop.service.Impl.HeaderServiceImpl;
import com.eshop.service.Impl.HomeServiceImpl;
import com.eshop.service.HomeService;
import com.eshop.service.Impl.LoginServiceImpl;
import com.eshop.service.Impl.RegisterServiceImpl;
import com.eshop.service.LoginService;
import com.eshop.service.RegisterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns ={"/home"})
public class HomeController extends HttpServlet {
    HeaderService headerService = new HeaderServiceImpl();
    HomeService homeService = new HomeServiceImpl();
    RegisterService registerService = new RegisterServiceImpl();
    LoginService loginService = new LoginServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String action = req.getParameter("action");

        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Map<String,String>> todayGoodsList = homeService.getTodayGoodsList();
        List<CategoryDetail> categoryDetailList = homeService.getCateList();
        List<CategoryDetail> categoryDetailListForHeader = headerService.getCateList();
        List<Goods> hotGoodsList = homeService.getHotGoodsList();

        req.setAttribute("todayGoodsList",todayGoodsList);
        req.setAttribute("categoryDetailList",categoryDetailList);
        req.setAttribute("categoryDetailListForHeader",categoryDetailListForHeader);
        req.setAttribute("hotGoodsList",hotGoodsList);

        req.getRequestDispatcher("/pages/home.jsp").forward(req, resp);
    }

    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("usernameReg");
        String password = req.getParameter("passwordReg");
        String passwordConfirm = req.getParameter("passwordRegConfirm");
        String age = req.getParameter("age");
        String userSex = req.getParameter("userSex");
        String email = req.getParameter("email");

        if (username == null || username.trim().length() == 0) {
            resp.getWriter().write("{\"reg\":false,\"msg\":\"用户名不能为空\"}");
            return;
        }
        else if (password == null || password.trim().length() == 0) {
            resp.getWriter().write("{\"reg\":false,\"msg\":\"密码不能为空\"}");
            return;
        }
        else if (passwordConfirm == null || passwordConfirm.trim().length() == 0) {
            resp.getWriter().write("{\"reg\":false,\"msg\":\"确认密码不能为空\"}");
            return;
        }
        else if (registerService.IfUsernameExists(username)){
            resp.getWriter().write("{\"reg\":false,\"msg\":\"用户名已存在\"}");
            return;
        }
        else if (!password.equals(passwordConfirm)){
            resp.getWriter().write("{\"reg\":false,\"msg\":\"第二次输入密码不同\"}");
            return;
        }   

        try {
            User user = new User(0,username,password,Integer.parseInt(age),Integer.parseInt(userSex),email,0);
            registerService.AddUser(user);
            User loginUser = loginService.getUserByUsernameAndPassword(user.getUser_name(),user.getUser_pass());
            req.getSession().setAttribute("_LOGIN_USER_",loginUser);
            resp.getWriter().write("{\"reg\":true}");
        }catch (Exception e){
            e.printStackTrace();
            resp.getWriter().write("{\"reg\":false,\"msg\":\"格式错误\"}");
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = loginService.getUserByUsernameAndPassword(username,password);
        if (user != null) {
            req.getSession().setAttribute("_LOGIN_USER_",user);
            resp.getWriter().write("{\"login\":true}");
        }
        else {
            resp.getWriter().write("{\"login\":false}");
        }
    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("_LOGIN_USER_");
        req.getSession().removeAttribute("cartGoodsList");
        index(req,resp);
    }
}
